package pers.qianyu.month_202102.date_20210208;

/**
 * 978. 最长湍流子数组
 * https://leetcode-cn.com/problems/longest-turbulent-subarray/
 * - 滑动窗口解法
 *
 * @author mizzle rain
 * @date 2021-02-08 16:56
 */
public class MaxTurbulenceSize2 {
    public int maxTurbulenceSize(int[] arr) {
        int ans = 1;
        int len = arr.length;
        int left = 0, right = 0;
        while (right < len - 1) {
            if (left == right) {
                if (arr[left] == arr[left + 1]) left++;
                right++;
            } else {
                if (arr[right - 1] > arr[right] && arr[right] < arr[right + 1] || arr[right - 1] < arr[right] && arr[right] > arr[right + 1])
                    right++;
                else left = right;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
